<?php
namespace app\manage\controller;

class AuditSdkInfo extends Base
{
    //sdk列表
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $id = input('id');
            $channelId = input('channel_id');

            $where['channel_id'] = ['>=', 5000];
            !empty($id) && $where['id'] = $id;
            !empty($channelId) && $where['channel_id'] = $channelId;

            if( ! $count=model('Channel')->where($where)->count() ){
               $this->layTable();
            }

            $select = model('Channel')
                ->where($where)
                ->order('id desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            foreach ($select as $k=>$v){
                $data[] = [
                    'channel_id' => $v['channel_id'],
                    'channel_name' => $v['channel_name'],
                    'channel_rate' => $v['channel_rate'],
                    'tax_rate' => $v['tax_rate'],
                    'pay_rate' => $v['pay_rate'],
                    'company' => $v['company'],
                    'reg_address' => $v['reg_address'],
                    'contact_phone' => $v['contact_phone'],
                    'contact_man' => $v['contact_man'],
                    'open_name' => $v['open_name'],
                    'open_bank' => $v['open_bank'],
                    'open_account' => $v['open_account'],
                    'credit_code' => $v['credit_code'],
                    'send_address' => $v['send_address'],
                    'contact_mobile' => $v['contact_mobile'],
                    'audit_time' => $v['audit_time'] ? date('Y-m-d', $v['audit_time']) : '',
                ];
            }

            $this->layTable(0, '渠道对账信息', $data, $count);
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        model('Channel')->save(input(), ['channel_id'=>input('channel_id')]) ? $this->layJson(0, '编辑成功') : $this->layJson(1, '编辑失败，请重试');
    }

    //删除
    public function del(){
        model('SdkBillRate')->destroy(input('id')) ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }

    //sdk自定义比例
    public function custom(){
        if(request()->isAjax()){
            if( ! $count=model('SdkBillRate')->count() ){
                $this->layTable();
            }

            $select = model('SdkBillRate')->select();
            $data = [];
            $channelIdName = model('Channel')->channelIdName();
            $gameIdName = model('Game')->gameIdName();
            foreach ($select as $k=>$v){
                $channelId = isset($channelIdName[ $v['channel_id'] ]) ? $channelIdName[ $v['channel_id'] ] : '';
                $gameId = isset($gameIdName[ $v['game_id'] ]) ? $gameIdName[ $v['game_id'] ] : '';
                $data[] = [
                    'id' => $v['id'],
                    'channel_id' => $v['channel_id'] . '：' . $channelId,
                    'game_id' => $v['game_id'] . '：' . $gameId,
                    'pay_rate' => $v['pay_rate'],
                    'channel_rate' => $v['channel_rate'],
                    'tax_rate' => $v['tax_rate'],
                ];
            }

            return $this->layTable(0, '自定义比例', $data, $count);
        }else{
            return view();
        }
    }

    public function add(){
        $data = input();
        $where['channel_id'] = $data['channel_id'];
        $where['game_id'] = $data['game_id'];
        if(model('SdkBillRate')->where($where)->count()){
            $this->layJson(0, '已存在该记录');
        }
        if( !model('SdkBillRate')->save($data) ){
            $this->layJson(1, '添加失败，请重试');
        }
        $this->layJson(0, '添加成功');
    }
}